package cn.hesion.mrreducejoin;
/**
 * ClassName: ReduceJoinMapper <br/>
 * Description: <br/>
 * date: 2021/2/3 11:39<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BEncoderStream;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @program: ClientDemo
 * @description:
 * @author: hesion
 * @create: 2021-02-03 11:39
 **/
public class ReduceJoinMapper extends Mapper<LongWritable, Text, Text, DeliverBean> {

    String name;
    DeliverBean bean = new DeliverBean();
    Text k = new Text();


    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        //1 获取输入文件切片
        FileSplit split = (FileSplit) context.getInputSplit();
        //2 获取输入文件名称
        name = split.getPath().getName();

    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 获取输入的数据
        String line = value.toString();
        // 判断当前读的是哪个文件
        if (name.startsWith("deliver_info")) {
            //表示是投递信息
            //2.1 切割
            String[] fields = line.split("\t");
            bean.setUserId(fields[0]);
            bean.setPositionId(fields[1]);
            bean.setDate(fields[2]);
            bean.setPositionName("");
            bean.setFlag("deliver");
            k.set(fields[1]);
        } else {
            //2.1 切割
            String[] fields = line.split("\t");
            bean.setPositionId(fields[0]);
            bean.setPositionName(fields[1]);
            bean.setUserId("");
            bean.setDate("");
            bean.setFlag("position");
            k.set(fields[0]);
        }
        context.write(k, bean);

    }
}
